/**
 * Created by weng on 2017/8/16.
 */

var data_temp = {};
var  temp_page_size= 15;

/**
 * 获取推送历史记录
 * @param info
 */
var getInfo = function () {
    console.log("获取信息");
    var obj = {};
    obj.startTime = $("#startTime").val();
    obj.endTime =  moment($("#endTime").val()).format('YYYY-MM-DD 23:59:59');

    $.ajax({
        url:'/hospital/getNewBornInfo',
        type:'post',
        dataType:'json',
        data:obj,
        success:function(data) {
            console.log("data",data);
            if (data.code != 200) {
                alert("查询错误");
                return;
            } else {
                if(data.info.length > 0){
                    data_temp = data.info
                    var temp_teble_info = data_temp.slice(0 , temp_page_size);
                    createPageMin(temp_page_size, 5, Math.ceil(data_temp.length), data_temp);
                    showTableInfo(temp_teble_info,"specialOrderList");
                }
            }
        }
    });
};



function createPageMin(pageSize, buttons, total,data_temp) {
    $("#pagination").empty();
    $('#pagination').Paging({pagesize:pageSize,count:total,toolbar:true,pageSizeList:[temp_page_size],
        callback:function(page,size,count){
            page = page - 1;
            var temp_teble_info = data_temp.slice(page  * size ,page * pageSize + size);
            showTableInfo(temp_teble_info,"specialOrderList");
        }});
}


function showTableInfo(data_arr,id){
    var html = "";
    var thead = $('#'+id + ' thead');
    var tbody = $('#'+id + ' tbody');

    tbody.empty();
    thead.empty();
    var htmlHead = "<tr>"
        + "<th>外围显示-时间：</th>"
        + "<th>外围显示-标题：</th>"
        + "<th>外围显示-内容：</th>"
        + "<th>操作：</th>"
        + "</tr>"
    htmlHead = $(htmlHead);
    thead.append(htmlHead);

    for(var i in data_arr){
        var time_des = data_arr[i].time;
        html += '<tr>' +
        '<td style="display: none" id="id'+ data_arr[i].id +'">'+  data_arr[i].id + '</td>'
        + '<td  id="title'+ data_arr[i].id +'">'+  moment(data_arr[i].time).format('YYYY-MM-DD HH:mm:ss') + '</td>'
        + '<td  id="title'+ data_arr[i].id +'">'+  data_arr[i].title + '</td>'
        +  '<td  id="msg:'+ data_arr[i].id +'">'+  data_arr[i].msg + '</td>'
        +   '<th>'
        +    '<a onclick="showNewBornDetail('+ data_arr[i].id +')">详细信息内容</a><br/>'
        +    '<a onclick="delNewBornInfo('+ data_arr[i].id +')">      删除信息</a>'
        +    '</th>';
            html += '</th>';
            html += '</tr>';
    }
    var tr = $(html);
    tbody.append(tr);
}


function showNewBornDetail(id) {
    console.log("id is",id)
    var url="/hospital/showNewBornDetail";
    $.ajax({
        url:url,
        type: 'POST',
        cache: false,
        async:false,
        data: {id:id},
        success:function(data){
            console.log(data)
            if(200 === data.code) {
                var data_arr = data.info;

                var htmlHead = "<tr>"
                    + "<th>标题：</th>"
                    +   '<td  id="value:'+ data_arr[0].id +'">'+  data_arr[0].detailTitle + '</td>'
                    + "</tr>";

                var html = "";
          /*      html += '<tr>'
                    + "<th>图片：</th>"
                 if(data_arr[0].image ){
                     html +=  '<td ><image style="height: 100px;width: 100px" src="'+ data_arr[0].image +'"/></td>'
                 }else {
                     html +=  '<td >无图片</td>'
                 }*/
                data_arr[0].detailContent =data_arr[0].detailContent.replace(/alt=""/g, "style='height:100px'");

                html += '</th>';
                html += '</tr>';

                html += '<tr>'
                    + "<th>内容：</th>"
                    +   '<td  id="value:'+ data_arr[0].id +'">'+  data_arr[0].detailContent + '</td>'
                html += '</th>';
                html += '</tr>';

                layer.open({
                    type: 1,
                    skin: 'layui-layer-rim', //加上边框
                    title:"重阳新生详细内容", //样式类名
                   // closeBtn: 0, //不显示关闭按钮
                    area: ['800px', '400px'],
                   // shadeClose: true, //开启遮罩关闭
                    content: "<table class='table table-bordered'>"
                    + "<thead>"+htmlHead+"</thead>"
                    + "<tbody>"+html+"</tbody>"
                    + "</table>"
                });
            }else {
                layer.msg(data.info );
            }
        }
    });
}



/**
 * 推送信息
 * @constructor
 */
function addNewBornInfo(){
    layer.msg('确定添加重阳故事内容？', {
        time: 0 //不自动关闭
        ,btn: ['确定', '取消']
        ,yes: function(index){
            var obj = {};
            editor.sync();
            obj.title = $("#title").val();
            obj.msg =$("#msg").val();
            obj.time =$("#time").html();
            obj.detailTitle =$("#detailTitle").val();
            obj.detailContent =$("#detailContent").val();

            if(!obj.title){
                return   layer.msg("请填写外围显示-标题");
            }else if(!obj.msg){
                return   layer.msg("请填写外围显示-内容");
            }else if(!obj.time){
                return   layer.msg("请填写正确的外围显示-时间");
            }else if(!obj.detailTitle){
                return   layer.msg("请填写具体内容-标题");
            }else if(!obj.detailContent){
                return   layer.msg("请填写具体内容-内容");
            }
            var formData = new FormData();
            formData.append('files',$("#imageSent")[0].files[0]);
            formData.append('title',obj.title);
            formData.append('msg',obj.msg);
            formData.append('time',obj.time );
            formData.append('detailTitle',obj.detailTitle );
            formData.append('detailContent',obj.detailContent );

            var url="/hospital/addNewBornInfo";
            $.ajax({
                url:url,
                type: 'POST',
                cache: false,
                data: formData,
                processData: false,
                contentType: false,
                success:function(data){
                    layer.close(index);
                    if(200 === data.code) {
                        layer.msg('添加成功', {
                            icon: 1,
                            time: 1000 //2秒关闭（如果不配置，默认是3秒）
                        }, function(){
                            location.reload();
                        });
                    }else {
                        layer.msg(data.info );
                    }
                }
            });
        }
    });
}


var delNewBornInfo = function (id) {
    layer.msg('你确定删除么？', {
        time: 0 //不自动关闭
        ,btn: ['确定', '取消']
        ,yes: function(index){
            var obj = {};
            obj.id = id;
            var url="/hospital/delNewBornInfo";
            $.ajax({
                url:url,
                type:'post',
                dataType:'json',
                data:obj,
                success:function(data){
                    layer.close(index);
                    if(200 === data.code) {
                        layer.msg('刪除成功', {
                            icon: 1,
                            time: 1000 //2秒关闭（如果不配置，默认是3秒）
                        }, function(){
                            location.reload();
                        });
                    }else {
                        layer.msg(data.info );
                    }
                }
            });
        }
    });
}